package com.penjin.android.message.utils;

import com.penjin.android.R;
import com.penjin.android.domain.PenjinEaseUser;
import com.penjin.android.message.domain.EaseUser;
import com.penjin.android.utils.BitmapUtils;
import com.penjin.android.utils.JsonUtils;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class EaseUserUtils {

    static EaseUI.EaseUserProfileProvider userProvider;
    static int DEFAULT_AVATARS[] = {R.drawable.pj_avartar_black, R.drawable.pj_avartar_blue, R.drawable.pj_avartar_gray, R.drawable.pj_avartar_green, R.drawable.pj_avartar_light_blue,
            R.drawable.pj_avartar_light_green, R.drawable.pj_avartar_orange, R.drawable.pj_avartar_purple, R.drawable.pj_avartar_mgreen, R.drawable.pj_avartar_red,
            R.drawable.pj_avartar_yellow,};

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * 根据username获取相应user
     *
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username) {
        if (userProvider != null)
            return userProvider.getUser(username);
        return new EaseUser(username);
    }

    /**
     * 设置用户头像
     *
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView) {
        EaseUser user = getUserInfo(username);
        try {
            int avatarResId = Integer.parseInt(user.getAvatar());
            Picasso.with(context).load(avatarResId).into(imageView);
        } catch (Exception e) {
            //正常的string路径
            Picasso.with(context).load(user.getAvatar()).placeholder(R.drawable.ease_default_avatar).into(imageView);
        }
    }

    /**
     * 设置用户昵称
     */
    public static void setUserNick(String username, TextView textView) {
        if (textView != null) {
            EaseUser user = getUserInfo(username);
            if (user != null && user.getNick() != null) {
                textView.setText(user.getNick());
            } else {
                textView.setText(username);
            }
        }
    }

    public static void setPenjinUserAvatar(Context context, PenjinEaseUser user, ImageView imageView) {
        if (JsonUtils.isJsonNull(user.getAvatar())) {//如果没有提供员工头像
            EaseUser u = getUserInfo(user.getUsername());//尝试使用即时通讯头像
            if (u == null) {
                int index = user.colorIndex;
                Picasso.with(context).load(DEFAULT_AVATARS[index]).priority(Picasso.Priority.LOW).into(imageView);
            } else {
                try {
                    int avatarResId = Integer.parseInt(user.getAvatar());
                    Picasso.with(context).load(avatarResId).priority(Picasso.Priority.LOW).into(imageView);
                } catch (Exception e) {
                    //正常的string路径
                    Picasso.with(context).load(u.getAvatar()).priority(Picasso.Priority.LOW).placeholder(R.drawable.ease_default_avatar).into(imageView);
                }
            }
        } else {
            Picasso.with(context).load(user.getAvatar()).priority(Picasso.Priority.LOW).into(imageView);
        }
    }

    public static void setSignature(Context context, PenjinEaseUser penjinUser, TextView signature) {
        EaseUser user = getUserInfo(penjinUser.getUsername());
        if (user == null) {
            signature.setVisibility(View.VISIBLE);
            signature.setText("员工尚未绑定App");
        } else {
            signature.setVisibility(View.GONE);
        }
    }
}
